<?php
namespace App\Http\Controllers\admin_system;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class AdController extends Controller{


    /*广告列表*/
    public function index(){
        $ads = DB::table('ad_info')->orderBy('ad_sort','desc')->get();
        $ads = (object) $ads;
        return view('admin_system/ad/index',['ads'=>$ads]);
    }


    /*关闭或显示*/
    public function close_or_show(Request $request){
        $adId       = $request->input('adId');
        $isDelete   = $request->input('isDelete');
        DB::table('ad_info')->where('ad_id',$adId)->update(['is_delete'=>$isDelete]);
        return $this->index($request);
    }


    /*删除广告信息*/
    public function delete(Request $request){
        $adId = $request->input('adId');
        DB::table('ad_info')->where('ad_id',$adId)->delete();
        return $this->index($request);
    }



    /*添加 或编辑广告*/
    public function edit(Request $request){

        $adId = $request->input('adId',0);
        if($adId > 0 ){
            $ad = DB::table('ad_info')->where('ad_id',$adId)->first();
        }

        if($adId == 0 || !$ad){
            $ad = [
                'ad_id'     => 0,
                'ad_img'    => 'http://iph.href.lu/750x1334',
                'ad_name'   => '',
                'ad_sort'   => ''
            ];
        }

        $ad = (object) $ad;
        return view('admin_system/ad/edit',['ad'=>$ad]);
    }


    /*添加广告动作*/
    public function add(Request $request){
        $adId = $request->input('ad_id');
        $data = [
            'ad_name'   => $request->input('ad_name'),
            'ad_sort'   => $request->input('ad_sort'),
        ];


        if($request->file('ad_img')){
            $imgInfo = $request->file('ad_img')->store('add-ad','web');
            $data['ad_img']  = "uploads/".$imgInfo;
        }

        if(!$adId){
            $info = DB::table('ad_info')->insert($data);
        }else{
            $info = DB::table('ad_info')->where('ad_id',$adId)->update($data);
        }
        return $this->index($request);
    }


}



